package ruifeng.zcj.dig.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.ui.shopping.bean.Category;
import ruifeng.zcj.dig.ui.view.XViewPager;
import ruifeng.zcj.mybase.base.App;
import ruifeng.zcj.mybase.base.BaseActivity;

/**
 * 存放有所Fragment的父类activity，存放多少activity由子类实现
 * Created by Administrator on 2016/9/3 0003.
 */
public abstract class DepositFragmentActivity extends BaseActivity {
    public static final String KEY_CLASS_NAME = "className";
    public static final String KEY_ARGS = "args";

    @Bind(R.id.activity_top_left)
    public ImageView mActivityTopLeft;
    @Bind(R.id.activity_top_center)
    public TextView mActivityTopCenter;
    @Bind(R.id.activity_top_right)
    public ImageView mActivityTopRight;
    @Bind(R.id.activity_top_right_text)
    public TextView mActivityTopRightText;
    @Bind(R.id.view_pager)
    public XViewPager mViewPager;

//    private InnerPageAdapter innerPageAdapter;

    //    private BaseFragmentViewPageAdapter mBaseFragmentViewPageAdapter;
    public ViewPagerFragmentAdapter mBaseFragmentViewPageAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_home_all;
    }

    @Override
    public void initView() {
        mActivityTopCenter.setText("测试");

        mBaseFragmentViewPageAdapter = new ViewPagerFragmentAdapter(App.getAppContext(), getSupportFragmentManager()) {
            @Override
            public void addPage(ArrayList<String> tabTitles, ArrayList<FragmentInfo> fragmentInfos) {
                DepositFragmentActivity.this.addPage(tabTitles, fragmentInfos);
            }
        };

        initContainer(mBaseFragmentViewPageAdapter);
        setScreenPageLimit();
//        onSetupTabAdapter(mBaseFragmentViewPageAdapter);
    }

    public void setScreenPageLimit() {

    }

    protected abstract void addPage(ArrayList<String> tabTitles, ArrayList<FragmentInfo> fragmentInfos);

    @OnClick({R.id.activity_top_left, R.id.activity_top_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_top_left:
                if (getCurrentItem() == 0) {
                    finish();
                } else {
                    setCurrentItem(0);
                }
                break;
            case R.id.activity_top_right:
                Toast.makeText(App.getAppContext(), "点击发布,切换Fragment，设置Title", Toast.LENGTH_SHORT).show();
                mActivityTopCenter.setText("切换");
                break;
        }
    }

    public void initContainer(final PagerAdapter pagerAdapter) {
        //mViewPager.setOffscreenPageLimit(limit);// 表示limit个界面之间来回切换都不会重新加载

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mViewPager != null) {
                    mViewPager.setAdapter(pagerAdapter);
                }
            }
        }, 100);
        //mViewPager.setAdapter(pagerAdapter);
    }

    public void setCurrentItem(int item) {
        mViewPager.setCurrentItem(item, false);
    }

    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }


    /**
     * 基类会根据不同的catalog展示相应的数据
     *
     * @param catalog 要显示的数据类别
     * @return
     */
    public Bundle getBundle(String catalog) {
        Bundle bundle = this.getIntent().getExtras();
        /*FragmentArgs args = (FragmentArgs) bundle.getSerializable("categoryBundle");
        Category.RowsBean rowsBean = (Category.RowsBean) args.get("category");
        showSnackBar("标题类型"+rowsBean.getSuppliertype());*/
       /* bundle.getBundle("categoryBundle");
        bundle.putString("11", catalog);*/
        return bundle;
    }

    public Bundle getBundle(int newType) {
        Bundle bundle = new Bundle();
        bundle.putInt("11", newType);
        return bundle;
    }
}
